package net.mikoo.seals.biz.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;

import net.mikoo.seals.biz.common.BaseServices;
import net.mikoo.seals.biz.mapper.CompanyMapper;
import net.mikoo.seals.biz.mapper.DevicesMapper;
import net.mikoo.seals.biz.mapper.PartyMapper;
import net.mikoo.seals.biz.mapper.StaffMapper;
import net.mikoo.seals.biz.mapper.WorkflowMapper;
import net.mikoo.seals.biz.mapper.WorkflowSettingMapper;
import net.mikoo.seals.biz.service.CompanyService;
import net.mikoo.seals.common.exception.DataNotFoundException;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.model.Company;
import net.mikoo.seals.model.CompanyQuery;
import net.mikoo.seals.model.Devices;
import net.mikoo.seals.model.DevicesQuery;
import net.mikoo.seals.model.Party;
import net.mikoo.seals.model.Staff;
import net.mikoo.seals.model.Workflow;
import net.mikoo.seals.model.WorkflowQuery;
import net.mikoo.seals.model.WorkflowSetting;
import net.mikoo.seals.model.WorkflowSettingQuery;
import net.mikoo.seals.utils.ValidatorUtil;

public class CompanyServiceImpl extends BaseServices implements CompanyService{
	
	@Autowired
	private PartyMapper partyMapper;
	
	@Autowired
	private StaffMapper staffMapper;

	@Autowired
	private CompanyMapper companyMapper;
	
	@Autowired
	private DevicesMapper devicesMapper;
	
	@Autowired
	private WorkflowMapper workflowMapper;
	
	@Autowired
	private WorkflowSettingMapper workflowSettingMapper;

	@Override
	public Company getDetail(Long companyId) {
		
		if(companyId == null) {
			throw new ParametersException("companyId不能为空");
		}
		
		Company company = companyMapper.selectInfoById(companyId);
		if(company == null) {
			throw new DataNotFoundException("company不存在");
		}
		
		DevicesQuery devicesQuery = new DevicesQuery();
		devicesQuery.setCompanyId(companyId);
		List<Devices> devicesList = devicesMapper.selectListByQuery(devicesQuery);
		
		company.setDevicesList(devicesList);
		
		WorkflowQuery workflowQuery = new WorkflowQuery();
		workflowQuery.setCompanyId(companyId);
		List<Workflow> workflowList = workflowMapper.selectListByQuery(workflowQuery);
		
		if(workflowList != null) {
			for(Workflow workflow : workflowList) {
				if(workflow == null) {
					continue;
				}
				
				WorkflowSettingQuery workflowSettingQuery = new WorkflowSettingQuery();
				workflowSettingQuery.setWorkflowId(workflow.getWorkflowId());
				List<WorkflowSetting> settingList = workflowSettingMapper.selectListByQuery(workflowSettingQuery);
				//workflow.setSettingList(settingList);
			}
		}
		company.setWorkFlowList(workflowList);
		return company;
	}

	@Override
	public Company getInfoByQuery(CompanyQuery query) {
		if(query == null) {
			throw new ParametersException("参数错误");
		}
		return companyMapper.selectInfoByQuery(query);
	}

	@Override
	public void modify(Company company) {
		
		if(company == null) {
			throw new ParametersException("company is null");
		}
		if(company.getCompanyId() == null) {
			throw new ParametersException("CompanyId is null");
		}
		
		if(StringUtils.isBlank(company.getPhoneNumber())) {
			throw new ParametersException("请输入公司电话号码");
		}
		
		if(!ValidatorUtil.isPhone(company.getPhoneNumber())) {
			throw new ParametersException("公司电话号码格式输入有误");
			
		}
		
		companyMapper.update(company);
	}

	@Override
	public void transferAdmin(Long companyId,Long fromPartyId,Long toPartyId) {
		
		if(companyId == null) {
			throw new ParametersException("CompanyId is null");
		}
		
		if(fromPartyId == null) {
			throw new ParametersException("fromPartyId is null");
		}
		
		if(toPartyId == null) {
			throw new ParametersException("toPartyId is null");
		}
		
		Party party = partyMapper.selectByPrimaryKey(toPartyId);
		if(party == null ) {
			throw new ParametersException("此人员不存在");
		}
		
		this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				
				Company company = new Company();
				company.setCompanyId(companyId);
				company.setUserLoginId(party.getUserLoginId());
				company.setPartyId(toPartyId);
				companyMapper.updateToTransferAdmin(company);
				
				Staff fromStaff = new Staff();
				fromStaff.setPartyId(fromPartyId);
				fromStaff.setIsSystem("N");
				staffMapper.updateToTransferAdmin(fromStaff);
				
				Staff toStaff = new Staff();
				toStaff.setPartyId(fromPartyId);
				toStaff.setIsSystem("Y");
				staffMapper.updateToTransferAdmin(toStaff);
				
			}
		});
		
	}

	@Override
	public List<Company> getListByPartyId(Long partyId) {
		if(partyId == null) {
			throw new IllegalArgumentException("partyId");
		}
		return companyMapper.selectByPartyId(partyId);
	}

	@Override
	public Company getSelectedByQuery(CompanyQuery query) {
		
		if(query == null) {
			throw new ParametersException("CompanyQuery is null");
		}
		
		if(query.getPartyId() == null) {
			throw new ParametersException("PartyId is null");
		}
		
		if(query.getCompanyId() == null) {
			throw new ParametersException("请选择公司");
		}
		
		Company company = companyMapper.selectSelectedByQuery(query);
		
		return company;
	}
}
